package org.matcha.daddy

import groovy.sql.Sql

class JdbcDemo {

    static void lvpOms() {

        //生成文件存放位置
        def file = new File('/home/yhf/Downloads', 'channel.sql')
        if (file.exists()) {
            file.delete()
        }

        //需要导出数据配置
        def lvpOmsDb = Sql.newInstance('jdbc:mysql:/ip:port/scheam',
                'user', 'password', 'com.mysql.cj.jdbc.Driver')

        def lvpOmsPosterQuerySql = 'select poster_url from poster where poster_id=?'

        lvpOmsDb.eachRow('select channel_id,\n' +
                '       uuid,\n' +
                '       directory,\n' +
                '       channel_name,\n' +
                '       is_grasp,\n' +
                '       create_datetime,\n' +
                '       update_datetime,\n' +
                '       definition_type,\n' +
                '       poster_id,\n' +
                '       channel_status,\n' +
                '       hdchannel_id,\n' +
                '       poster1,\n' +
                '       poster2,\n' +
                '       is_local,\n' +
                '       is_base,\n' +
                '       is_vip,\n' +
                '       channel_desc,\n' +
                '       vertical_poster_id,\n' +
                '       tv_channel_logo,\n' +
                '       phone_channel_logo,\n' +
                '       block_channel_logo,\n' +
                '       personal_channel_logo,\n' +
                '       is_auto,\n' +
                '       default_playbill_class,\n' +
                '       is_carousel,\n' +
                '       is_support_look_back,\n' +
                '       out_channel_id,\n' +
                '       hw_channel_id,\n' +
                '       hw_media_id,\n' +
                '       is24H\n' +
                'from channel') {
            tp ->
                def channel_id = tp.channel_id
                def uuid = covertStr(tp.uuid)
                def channel_properties = "'radio'"
                def channel_status = covertStr(tp.channel_status)
                def directory = covertStr(tp.directory)
                def channel_name = covertStr(tp.channel_name)
                def definition_type = covertStr(tp.definition_type)
                def is_grasp = tp.is_grasp
                def is_local = tp.is_local
                def is_base = tp.is_base
                def is_vip = tp.is_vip
                def is_auto = tp.is_auto
                def is_carousel = tp.is_carousel
                def is_support_look_back = tp.is_support_look_back
                def channel_desc = covertStr(tp.channel_desc)
                def horizontal_poster1
                def horizontal_poster2
                if (null != tp.poster_id) {
                    def posterQueryParams = [tp.poster_id]
                    def hp = lvpOmsDb.firstRow lvpOmsPosterQuerySql, posterQueryParams
                    if (!hp.isEmpty()) {
                        horizontal_poster1 = covertStr(hp.get('poster_url'))
                    }
                }
                def vertical_poster
                if (null != tp.vertical_poster_id) {
                    def posterQueryParams = [tp.vertical_poster_id]
                    def hp = lvpOmsDb.firstRow lvpOmsPosterQuerySql, posterQueryParams
                    if (!hp.isEmpty()) {
                        vertical_poster = covertStr(hp.get('poster_url'))
                    }
                }
                def channel_logo = covertStr(tp.tv_channel_logo)
                def channel_img_logo = covertStr(tp.tv_channel_logo)
//                def create_by = "'admin'"
//                def create_time
                def update_by
                def update_time
                def district_codes
                def lvpsInsertSql = "INSERT INTO channel (channel_id, uuid, channel_properties, channel_status, directory, channel_name, definition_type,is_grasp, is_local, is_base, is_vip, is_auto, is_carousel, is_support_look_back, channel_desc,horizontal_poster1, horizontal_poster2, vertical_poster, channel_logo, channel_img_logo, create_by, create_time, update_by, update_time, district_codes) VALUES ( ${channel_id} , ${uuid} , ${channel_properties} , ${channel_status} , ${directory} , ${channel_name} , ${definition_type} , ${is_grasp} , ${is_local} , ${is_base} , ${is_vip} , ${is_auto} , ${is_carousel} , ${is_support_look_back} , ${channel_desc} , ${horizontal_poster1} , ${horizontal_poster2} , ${vertical_poster} , ${channel_logo} , ${channel_img_logo} , 'admin' , now() , ${update_by} , ${update_time} , ${district_codes});"
                file << lvpsInsertSql + '\n'
        }

        lvpOmsDb.close()
    }

    static void main(String[] args) {
        lvpOms()
    }

    static def covertStr(def str) {
        return str == null ? null : "'" + str + "'"
    }

}
